<?php
class Cl_BbsPost extends WdkClass 
{
	protected
		/******************************************************/
		//	원본 테이블 정보
		/******************************************************/
		$bbs_post_seq			= 0		,	//
		$fk_bbs_seq				= 0		,	//	
		$fk_category_seq		= 0		,	//	
		$fk_user_seq			= 0		,	//	
		$bbs_post_user_name		= ''	,	//	
		$bbs_post_user_email	= ''	,	//	
		$bbs_post_user_mobile	= ''	,	//	
		$bbs_post_user_passwd	= ''	,	//	
		$bbs_post_title			= ''	,	//	
		$bbs_post_content		= ''	,	//	
		$bbs_post_files			= ''	,	//	
		$bbs_post_secure_flag	= 'F'	,	//	
		$bbs_post_notice_flag	= 'F'	,	//	
		$bbs_post_hits			= 0		,	//	
		$bbs_post_insert_time	= 0		,	//	
		$bbs_post_del_flag		= 'F'	;	//	
		/******************************************************/
		//	추가 정보
		/******************************************************/
	
	/**
	 * 	
	 */
	function __construct()
	{
		parent::__construct();
		self::getDbo();
	}
	
	/**
	 *	
	 */
	public function __init($data)
	{
		parent::__init($data);
		self::dformat();
	}
	
	/**
	 *	
	 */
	public function load($bbs_post_seq)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoad($bbs_post_seq));
	}
	
	/**
	 *	
	 */
	public function dformat()
	{
		if(is_string($this->bbs_post_files))
		{
			$this->bbs_post_files = (array) json_decode($this->bbs_post_files, 1);
		}
	}
	
	/**
	 * 
	 */
	public function autoUpdate($log='')
	{
		if((int)$this->bbs_post_seq > 0)
		{
			$this->update($log);
		}
		else
		{
			$this->insert($log);
		}
	}
	
	/**
	 *	
	 */
	public function insert($log='Insert')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		$pwd = Wdk::helper('Passwd');
		/*------------------------------------------------------------*/
		
		$this->bbs_post_insert_time	= time();
		$this->bbs_post_del_flag		= 'F';
		if((int)$this->fk_user_seq == 0)
		{
			$this->bbs_post_user_passwd	= $pwd->encrypt($this->bbs_post_user_passwd);
		}
		
		$data = array(	'fk_bbs_seq'			=>	$this->fk_bbs_seq,
						'fk_category_seq'		=>	$this->fk_category_seq,
						'fk_user_seq'			=>	$this->fk_user_seq,
						'bbs_post_user_name'	=>	$this->bbs_post_user_name,
						'bbs_post_user_email'	=>	$this->bbs_post_user_email,
						'bbs_post_user_mobile'	=>	$this->bbs_post_user_mobile,
						'bbs_post_user_passwd'	=>	$this->bbs_post_user_passwd,
						'bbs_post_title'		=>	$this->bbs_post_title,
						'bbs_post_content'		=>	$this->bbs_post_content,
						'bbs_post_files'		=>	json_encode((array)$this->bbs_post_files),
						'bbs_post_secure_flag'	=>	$this->bbs_post_secure_flag,
						'bbs_post_notice_flag'	=>	$this->bbs_post_notice_flag,
						'bbs_post_hits'			=>	$this->bbs_post_hits,
						'bbs_post_insert_time'	=>	$this->bbs_post_insert_time,
						'bbs_post_del_flag'		=>	$this->bbs_post_del_flag
					);
		$this->bbs_post_seq = $dbo->insert($data);
		$this->backup($log);
	}
	
	/**
	 * 
	 */
	public function update($log='update')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		$pwd = Wdk::helper('Passwd');
		/*------------------------------------------------------------*/
		if((int)$this->fk_user_seq == 0)
		{
			$this->bbs_post_user_passwd	= $pwd->encrypt($this->bbs_post_user_passwd);
		}
		
		$data = array(	'fk_category_seq'		=>	$this->fk_category_seq,
						'fk_user_seq'			=>	$this->fk_user_seq,
						'bbs_post_user_name'	=>	$this->bbs_post_user_name,
						'bbs_post_user_email'	=>	$this->bbs_post_user_email,
						'bbs_post_user_mobile'	=>	$this->bbs_post_user_mobile,
						'bbs_post_user_passwd'	=>	$this->bbs_post_user_passwd,
						'bbs_post_title'		=>	$this->bbs_post_title,
						'bbs_post_content'		=>	$this->bbs_post_content,
						'bbs_post_files'		=>	json_encode((array)$this->bbs_post_files),
						'bbs_post_secure_flag'	=>	$this->bbs_post_secure_flag,
						'bbs_post_notice_flag'	=>	$this->bbs_post_notice_flag,
						'bbs_post_hits'			=>	$this->bbs_post_hits
					);
		$dbo->update($data, array("bbs_post_seq = ".$this->bbs_post_seq));
		$this->backup($log);
	}
	
	/**
	 * 
	 */
	public function delete($log='delete')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->delete(array("bbs_post_seq = ".$this->bbs_post_seq));
		$this->backup($log);
	}
	
	/**
	 *	
	 */
	private function backup($log='')
	{
// 		/*------------------------------------------------------------*/
// 		$dbo = self::getDbo();
// 		/*------------------------------------------------------------*/
// 		$dbo->log($this->user_seq, $log, $this->backup_data);
	}
	
	/**
	 * 
	 */
	public function getDbo()
	{
		return Wdk::model(substr(__CLASS__, 3));
	}
}